package com.robot.mvvm.ui.work

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import com.robot.base.base.activity.GLBaseFragment
import com.robot.base.configs.AppConstants
import com.robot.base.model.QuestionDetailDTOBean
import com.robot.base.model.UploadFileBean
import com.robot.mvvm.BR
import com.robot.mvvm.R
import com.robot.mvvm.databinding.AppFragmentRepairWorkOrderDetailInfoBinding
import com.robot.mvvm.ui.work.adapter.ImgAdapter
import com.robot.mvvm.vm.work.RepairWorkOrderInfoViewModel

class RepairWorkOrderInfoFragment :
    GLBaseFragment<AppFragmentRepairWorkOrderDetailInfoBinding, RepairWorkOrderInfoViewModel>() {

    var questionId: String = ""
    var mImgAdapter: ImgAdapter? = null

    companion object {
        fun newInstance(questionId: String): RepairWorkOrderInfoFragment {
            val fragment = RepairWorkOrderInfoFragment()
            val bundle = Bundle()
            bundle.putString(AppConstants.BundleKey.QUESTION_ID, questionId)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun initParam() {
        super.initParam()
        questionId = arguments?.getString(AppConstants.BundleKey.QUESTION_ID) ?: ""
    }

    override fun initContentView(
        inflater: LayoutInflater?,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): Int = R.layout.app_fragment_repair_work_order_detail_info

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        initAdapter()
        viewModel.getQuestionDetail(questionId)
    }

    fun initAdapter() {
        mImgAdapter = ImgAdapter()

        binding.mRvImg.apply {
            layoutManager = GridLayoutManager(requireContext(), 3)
            adapter = mImgAdapter
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.uc.refreshDataEvent.observe(this){
            mImgAdapter?.setNewInstance(it.fileList as MutableList<UploadFileBean>?)
        }
    }

}